package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.component.CityAndMarketDialog;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.RevampStoreService;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ToastUtil;

public class SupFillInfo2Activity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {
	private String tag = "SupFillInfo2Activity";
	private boolean isUpdate = false;
	
	private ImageView ivReturn;
	private TextView tvTitle, tvCity, tvMarket, tvNext, tvJustNow;
	private RelativeLayout rlCity, rlMarket;

	private Supplier supplier;
	private GsonBean gsonBean;
	private CityAndMarketDialog showCityAndMarketDialog;
	private RevampStoreService revampStoreService = (RevampStoreService) MyServiceFactory
			.getInstance(MyServiceFactory.REVAMP_STORE);
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generate d method stub
		super.onCreate(savedInstanceState);
		setup();
	}

	private void setup() {
		setContentView(R.layout.sup_fill_info2);
		supplier = SupIndexShared.getSupplier();

		ivReturn = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvCity = (TextView) findViewById(R.id.tvCity_fill_info2);
		tvMarket = (TextView) findViewById(R.id.tvMarket_fill_info2);
		tvNext = (TextView) findViewById(R.id.tvNext_fill_info2);
		tvJustNow = (TextView) findViewById(R.id.tvJustNow_fill_info2);
		rlCity = (RelativeLayout) findViewById(R.id.rlCiti_fill_info2);
		rlMarket = (RelativeLayout) findViewById(R.id.rlMarket_fill_info2);
		if (supplier != null) {
			tvCity.setText(supplier.getSupCity());
			tvMarket.setText(supplier.getsupMarket());
		}

		tvTitle.setText("完善信息-卖场");
		ivReturn.setOnClickListener(onClick);
		rlCity.setOnClickListener(onClick);
		rlMarket.setOnClickListener(onClick);
		tvNext.setOnClickListener(onClick);
		tvJustNow.setOnClickListener(onClick);
		normalDataLoader.setOnloadDataListener(this);
		showCityAndMarketDialog = new CityAndMarketDialog(this,
				normalDataLoader.getHandler(), tvCity, tvMarket);
		Log.v(tag, "Task Id:"+ this.getTaskId());
	}

	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.ivPublicTitleReturn:
				finish();
				break;
			case R.id.rlCiti_fill_info2:
				showCityAndMarketDialog.showCity();
				break;
			case R.id.rlMarket_fill_info2:
				showCityAndMarketDialog.showMarket();
				break;
			case R.id.tvNext_fill_info2:
				updateSup();
				Intent intentNext = new Intent(SupFillInfo2Activity.this,
						SupFillInfo3Activity.class);
				startActivity(intentNext);
				break;
			case R.id.tvJustNow_fill_info2:
				updateSup();
				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				Intent intentMore = new Intent(SupFillInfo2Activity.this,
						CargoActivity.class);
				startActivity(intentMore);
				finish();
				break;

			}
		}
	};

	private boolean updateSup() {

		boolean toUpdate = false;
		try {
			if (!supplier.getSupCity().equals(tvCity.getText().toString())) {
				supplier.setSupCity(tvCity.getText().toString());
				toUpdate = true;
			}
			if (!supplier.getsupMarket().equals(tvMarket.getText().toString())) {
				supplier.setsupMarket(tvMarket.getText().toString());
				toUpdate = true;
			}

			if (toUpdate) {
				normalDataLoader.load();
			}
		} catch (Exception e) {
//			tvCity.getText().toString();
			if(supplier == null){
				supplier = new Supplier();
			}
			supplier.setSupCity(tvCity.getText().toString());
			supplier.setsupMarket(tvMarket.getText().toString());
			toUpdate = true;
		}
		return toUpdate;
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		ToastUtil.show(SupFillInfo2Activity.this, "正在更新数据");
		tvNext.setClickable(false);
		tvJustNow.setClickable(false);
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		gsonBean = revampStoreService.execute(supplier);
		if(CheckStateUtil.check(gsonBean)){
			msg.what = MyStateUtil.UPLOAD_RESULT;
		}else{
			msg.what = MyStateUtil.UPLOAD_ERROR;
		}
		Log.v(tag, DataUtil.gson.toJson(gsonBean));
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		
		switch (state) {
		case MyStateUtil.UPLOAD_RESULT:
			ToastUtil.show(getBaseContext(), "上传成功");
			SupIndexShared.saveSupplier(DataUtil.gson
					.toJson(supplier));
			break;
		case MyStateUtil.UPLOAD_ERROR:
			ToastUtil.show(getBaseContext(), "上传出错，请重新提交");
			break;
		case MyStateUtil.RETURN_FAIL:
			ToastUtil.show(getBaseContext(), "无对应数据");
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			break;
		}
		
		tvNext.setClickable(true);
		tvJustNow.setClickable(true);
	}
}
